#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#endif
#include <hld/dis/vpo.h>


#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"

#include "win_timer_set.h"

#ifdef SUPPORT_CAS9
#include "conax_ap/win_ca_mmi.h"
#endif

#ifdef SUPPORT_CAS7
#include "conax_ap7/win_ca_mmi.h"
#endif

#ifdef SUPPORT_CAS_A
#include "win_com_menu_define.h"
#endif

#ifdef VMX_PVR_SUPPORT
#include "vmx_ap/vmx_cas.h"
#endif

#if Maker_GUI_OFF
#include "win_submenu.h"
#endif

/*******************************************************************************
*	WINDOW's objects declaration
*******************************************************************************/

extern CONTAINER g_win_timerlist;
#if Maker_GUI_OFF
extern SCROLL_BAR	timer_scrollbar;
#endif
extern CONTAINER con_timer0;
extern CONTAINER con_timer1;
extern CONTAINER con_timer2;
extern CONTAINER con_timer3;
extern CONTAINER con_timer4;
extern CONTAINER con_timer5;
extern CONTAINER con_timer6;
extern CONTAINER con_timer7;

extern TEXT_FIELD txt_timer_num0;
extern TEXT_FIELD txt_timer_num1;
extern TEXT_FIELD txt_timer_num2;
extern TEXT_FIELD txt_timer_num3;
extern TEXT_FIELD txt_timer_num4;
extern TEXT_FIELD txt_timer_num5;
extern TEXT_FIELD txt_timer_num6;
extern TEXT_FIELD txt_timer_num7;

extern TEXT_FIELD txt_timer_mode0;
extern TEXT_FIELD txt_timer_mode1;
extern TEXT_FIELD txt_timer_mode2;
extern TEXT_FIELD txt_timer_mode3;
extern TEXT_FIELD txt_timer_mode4;
extern TEXT_FIELD txt_timer_mode5;
extern TEXT_FIELD txt_timer_mode6;
extern TEXT_FIELD txt_timer_mode7;

extern TEXT_FIELD txt_timer_service0;
extern TEXT_FIELD txt_timer_service1;
extern TEXT_FIELD txt_timer_service2;
extern TEXT_FIELD txt_timer_service3;
extern TEXT_FIELD txt_timer_service4;
extern TEXT_FIELD txt_timer_service5;
extern TEXT_FIELD txt_timer_service6;
extern TEXT_FIELD txt_timer_service7;

extern TEXT_FIELD txt_timer_cont0;
extern TEXT_FIELD txt_timer_cont1;
extern TEXT_FIELD txt_timer_cont2;
extern TEXT_FIELD txt_timer_cont3;
extern TEXT_FIELD txt_timer_cont4;
extern TEXT_FIELD txt_timer_cont5;
extern TEXT_FIELD txt_timer_cont6;
extern TEXT_FIELD txt_timer_cont7;

extern TEXT_FIELD txt_timer_date0;
extern TEXT_FIELD txt_timer_date1;
extern TEXT_FIELD txt_timer_date2;
extern TEXT_FIELD txt_timer_date3;
extern TEXT_FIELD txt_timer_date4;
extern TEXT_FIELD txt_timer_date5;
extern TEXT_FIELD txt_timer_date6;
extern TEXT_FIELD txt_timer_date7;

extern TEXT_FIELD txt_timer_dur0;
extern TEXT_FIELD txt_timer_dur1;
extern TEXT_FIELD txt_timer_dur2;
extern TEXT_FIELD txt_timer_dur3;
extern TEXT_FIELD txt_timer_dur4;
extern TEXT_FIELD txt_timer_dur5;
extern TEXT_FIELD txt_timer_dur6;
extern TEXT_FIELD txt_timer_dur7;

extern TEXT_FIELD txt_timer_line0;
extern TEXT_FIELD txt_timer_line1;
extern TEXT_FIELD txt_timer_line2;
extern TEXT_FIELD txt_timer_line3;
extern TEXT_FIELD txt_timer_line4;
extern TEXT_FIELD txt_timer_line5;
extern TEXT_FIELD txt_timer_line6;
extern TEXT_FIELD txt_timer_line7;


static VACTION timerlst_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT timerlst_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION timerlst_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT timerlst_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

#if Maker_GUI_OFF
static PRESULT 	win_timer_list_unkown_act_proc(POBJECT_HEAD pObj,VACTION act);
BOOL timermenu_set_items(sub_menu_t* sub_menu);
UINT8 timermenu_item_attr(sub_menu_t* sub_menu, UINT32 index);
BOOL timermenu_scroll_down(sub_menu_t* sub_menu);
BOOL timermenu_set_one_page(sub_menu_t* sub_menu);
void timermenu_set_item_attr(UINT32 itemidx, UINT8 bAttr);


sub_menu_t* timer_menu_desc;

#endif


#define WIN_SH_IDX	  WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_02_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_01_HD

#define TXTI_SH_IDX   WSTL_BUTTON_01_FG_HD//WSTL_MIXBACK_BLACK_IDX_HD
#define TXTI_HL_IDX   WSTL_BUTTON_02_FG_HD//WSTL_MIXBACK_BLACK_IDX_HD
#define TXTI_SL_IDX   WSTL_BUTTON_01_FG_HD//WSTL_MIXBACK_BLACK_IDX_HD
#define TXTI_GRY_IDX  WSTL_BUTTON_01_FG_HD//WSTL_MIXBACK_BLACK_IDX_HD

#define TXTN_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXTN_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXTN_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXTN_GRY_IDX  WSTL_BUTTON_01_FG_HD

#ifndef SD_UI
#if Maker_GUI_ON
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	Maker_GUI_SM_L
#define	W_T     	Maker_GUI_SM_T
#define	W_W     	Maker_GUI_SM_W
#define	W_H     	Maker_GUI_SM_H
#endif
#else
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif
#endif

#if Maker_GUI_OFF
#define SCB_T 		Maker_GUI_SM_T//150
#define SCB_W 		12//sharon 18
#define SCB_H 		Maker_GUI_SM_H - Maker_GUI_MM_T //323 //313
#define SCB_L 		Maker_GUI_SM_L - 20 + W_W - SCB_W

#define LIST_BAR_SH_IDX  WSTL_SCROLLBAR_01_8BIT//sharon WSTL_SCROLLBAR_01_HD
#define LIST_BAR_HL_IDX  WSTL_SCROLLBAR_01_8BIT//sharon WSTL_SCROLLBAR_01_HD

#define LIST_BAR_MID_RECT_IDX  	WSTL_NOSHOW_IDX
#define LIST_BAR_MID_THUMB_IDX  	WSTL_SCROLLBAR_02_8BIT//sharon WSTL_SCROLLBAR_02_HD

#define C_TIMERMENU_PAGE      7

#endif
#if Maker_GUI_ON
#define CON_L		(W_L  + 10)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 72 - (12 + 4))
#define CON_H		30
#define CON_GAP 	6

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4

#define ITEM_L 		CON_L
#define ITEM0_T 	(W_T + 2)
#define ITEM_W  	CON_W//(W_W - 60)
#define ITEM_H  	35
#define ITEM_GAP 	6

#else
#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 35)//(W_W - 60)
#define CON_H		40
#define CON_GAP		12

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4

#define ITEM_L 		CON_L
#define ITEM0_T 	(W_T + 2)
#define ITEM_W  	CON_W//(W_W - 60)
#define ITEM_H  	40
#define ITEM_GAP 	12
#endif


#define ITEM_IDX_L  (ITEM_L + 10)
#define ITEM_IDX_W  25//40
#define ITEM_MODE_L  (ITEM_IDX_L + ITEM_IDX_W)
#define ITEM_MODE_W  135//80
#define ITEM_SERV_L  (ITEM_MODE_L + ITEM_MODE_W)
#define ITEM_SERV_W  0
#define ITEM_CONT_L  (ITEM_SERV_L + ITEM_SERV_W)
#define ITEM_CONT_W  160
#define ITEM_DATE_L  (ITEM_CONT_L + ITEM_CONT_W)
#define ITEM_DATE_W  245//240
#define ITEM_DURA_L  (ITEM_DATE_L + ITEM_DATE_W)
#define ITEM_DURA_W  80//100
#else
#define	W_L     	105//384
#define	W_T     	57//138
#define	W_W     	482
#define	W_H     	370

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)
#define CON_H		32
#define CON_GAP		12

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4

#define ITEM_L 		CON_L
#define ITEM0_T 	(W_T + 2)
#define ITEM_W  	(W_W - 60)
#define ITEM_H  	32
#define ITEM_GAP 	12


#define ITEM_IDX_L  (ITEM_L + 10)
#define ITEM_IDX_W  22
#define ITEM_MODE_L  (ITEM_IDX_L + ITEM_IDX_W)
#define ITEM_MODE_W  57
#define ITEM_SERV_L  (ITEM_MODE_L + ITEM_MODE_W)
#define ITEM_SERV_W  0
#define ITEM_CONT_L  (ITEM_SERV_L + ITEM_SERV_W)
#define ITEM_CONT_W  107
#define ITEM_DATE_L  (ITEM_CONT_L + ITEM_CONT_W)
#define ITEM_DATE_W  162
#define ITEM_DURA_L  (ITEM_DATE_L + ITEM_DATE_W)
#define ITEM_DURA_W  60

#endif

#if Maker_GUI_OFF
#define LDEF_LISTBAR(root,varScb,page,l,t,w,h)	\
	DEF_SCROLLBAR(varScb, &root, &con_timer0, C_ATTR_ACTIVE, 0, \
		0, 0, 0, 0, 0, l, t, w, h, LIST_BAR_SH_IDX, LIST_BAR_HL_IDX, LIST_BAR_SH_IDX, LIST_BAR_SH_IDX, \
		NULL, NULL, BAR_VERT_AUTO | SBAR_STYLE_RECT_STYLE, page, LIST_BAR_MID_THUMB_IDX, LIST_BAR_MID_RECT_IDX, \
		0, 20, w, h - 40, 100, 1)

LDEF_LISTBAR(g_win_timerlist,timer_scrollbar,C_TIMERMENU_PAGE,SCB_L,SCB_T, SCB_W, SCB_H)		
#endif


#define LDEF_TXTI(root,varTxt,nxtObj,l,t,w,h)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXTI_SH_IDX,TXTI_HL_IDX,TXTI_SL_IDX,TXTI_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,NULL)

#define LDEF_TXTN(root,varTxt,nxtObj,l,t,w,h)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,NULL)

#define LDEF_CON(root, varCon,nxtObj,ID,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    timerlst_item_keymap,timerlst_item_callback,  \
    conobj, ID,1)


#define LDEF_TIMES_ITEM(root,varCon,nextobj,id,idt,idd,varIdx,varMode,varServ,varCont,varDate,varDur,varLine,t)  \
	LDEF_CON(root, varCon, nextobj, id,idt, idd,     ITEM_L,  	  t, ITEM_W, ITEM_H,&varIdx, 1)	\
    LDEF_TXTI(&varCon, varIdx, &varMode,  			 ITEM_IDX_L,  t, ITEM_IDX_W, ITEM_H)    \
    LDEF_TXTN(&varCon, varMode, &varServ,			 ITEM_MODE_L, t, ITEM_MODE_W, ITEM_H)    \
    LDEF_TXTN(&varCon, varServ, &varCont,			 ITEM_SERV_L, t, ITEM_SERV_W, ITEM_H)    \
    LDEF_TXTN(&varCon, varCont, &varDate, 			 ITEM_CONT_L, t, ITEM_CONT_W, ITEM_H)    \
    LDEF_TXTN(&varCon, varDate, &varDur, 			 ITEM_DATE_L, t, ITEM_DATE_W, ITEM_H)    \
    LDEF_TXTN(&varCon, varDur,  NULL/*&varLine*/,      			 ITEM_DURA_L, t, ITEM_DURA_W, ITEM_H)	\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,CON_L + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)


#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    timerlst_keymap,timerlst_callback,  \
    nxtObj, focusID,0)


#if Maker_GUI_OFF
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer0,&con_timer1,1,7,2,txt_timer_num0,txt_timer_mode0,txt_timer_service0,txt_timer_cont0,txt_timer_date0,txt_timer_dur0,txt_timer_line0,ITEM0_T + (ITEM_H + ITEM_GAP)*0)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer1,&con_timer2,2,1,3,txt_timer_num1,txt_timer_mode1,txt_timer_service1,txt_timer_cont1,txt_timer_date1,txt_timer_dur1,txt_timer_line1,ITEM0_T + (ITEM_H + ITEM_GAP)*1)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer2,&con_timer3,3,2,4,txt_timer_num2,txt_timer_mode2,txt_timer_service2,txt_timer_cont2,txt_timer_date2,txt_timer_dur2,txt_timer_line2,ITEM0_T + (ITEM_H + ITEM_GAP)*2)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer3,&con_timer4,4,3,5,txt_timer_num3,txt_timer_mode3,txt_timer_service3,txt_timer_cont3,txt_timer_date3,txt_timer_dur3,txt_timer_line3,ITEM0_T + (ITEM_H + ITEM_GAP)*3)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer4,&con_timer5,5,4,6,txt_timer_num4,txt_timer_mode4,txt_timer_service4,txt_timer_cont4,txt_timer_date4,txt_timer_dur4,txt_timer_line4,ITEM0_T + (ITEM_H + ITEM_GAP)*4)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer5,&con_timer6,6,5,7,txt_timer_num5,txt_timer_mode5,txt_timer_service5,txt_timer_cont5,txt_timer_date5,txt_timer_dur5,txt_timer_line5,ITEM0_T + (ITEM_H + ITEM_GAP)*5)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer6,NULL		,7,6,1,txt_timer_num6,txt_timer_mode6,txt_timer_service6,txt_timer_cont6,txt_timer_date6,txt_timer_dur6,txt_timer_line6,ITEM0_T + (ITEM_H + ITEM_GAP)*6)
#else
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer0,&con_timer1,1,8,2,txt_timer_num0,txt_timer_mode0,txt_timer_service0,txt_timer_cont0,txt_timer_date0,txt_timer_dur0,txt_timer_line0,ITEM0_T + (ITEM_H + ITEM_GAP)*0)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer1,&con_timer2,2,1,3,txt_timer_num1,txt_timer_mode1,txt_timer_service1,txt_timer_cont1,txt_timer_date1,txt_timer_dur1,txt_timer_line1,ITEM0_T + (ITEM_H + ITEM_GAP)*1)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer2,&con_timer3,3,2,4,txt_timer_num2,txt_timer_mode2,txt_timer_service2,txt_timer_cont2,txt_timer_date2,txt_timer_dur2,txt_timer_line2,ITEM0_T + (ITEM_H + ITEM_GAP)*2)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer3,&con_timer4,4,3,5,txt_timer_num3,txt_timer_mode3,txt_timer_service3,txt_timer_cont3,txt_timer_date3,txt_timer_dur3,txt_timer_line3,ITEM0_T + (ITEM_H + ITEM_GAP)*3)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer4,&con_timer5,5,4,6,txt_timer_num4,txt_timer_mode4,txt_timer_service4,txt_timer_cont4,txt_timer_date4,txt_timer_dur4,txt_timer_line4,ITEM0_T + (ITEM_H + ITEM_GAP)*4)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer5,&con_timer6,6,5,7,txt_timer_num5,txt_timer_mode5,txt_timer_service5,txt_timer_cont5,txt_timer_date5,txt_timer_dur5,txt_timer_line5,ITEM0_T + (ITEM_H + ITEM_GAP)*5)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer6,&con_timer7,7,6,8,txt_timer_num6,txt_timer_mode6,txt_timer_service6,txt_timer_cont6,txt_timer_date6,txt_timer_dur6,txt_timer_line6,ITEM0_T + (ITEM_H + ITEM_GAP)*6)
LDEF_TIMES_ITEM(&g_win_timerlist,con_timer7,NULL          ,8,7,1,txt_timer_num7,txt_timer_mode7,txt_timer_service7,txt_timer_cont7,txt_timer_date7,txt_timer_dur7,txt_timer_line7,ITEM0_T + (ITEM_H + ITEM_GAP)*7)
#endif

#if Maker_GUI_OFF
LDEF_WIN(g_win_timerlist,&timer_scrollbar,W_L,W_T,W_W,W_H,1)
#else
LDEF_WIN(g_win_timerlist,&con_timer0,W_L,W_T,W_W,W_H,1)
#endif


/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/


#if Maker_GUI_OFF
CONTAINER* timers_conts[] =
{
    &con_timer0,
    &con_timer1,
    &con_timer2,
    &con_timer3,
    &con_timer4,
    &con_timer5,
    &con_timer6
};
#else
CONTAINER* timers_conts[] =
{
    &con_timer0,
    &con_timer1,
    &con_timer2,
    &con_timer3,
    &con_timer4,
    &con_timer5,
    &con_timer6,
    &con_timer7
};
#endif

UINT16	TimerModeStr_id[]  = 
{
		RS_COMMON_OFF,
		RS_SYSTEM_TIMERSET_ONCE,
		RS_SYSTEM_TIMERSET_DAILY,
		RS_SYSTEM_TIMERSET_WEEKLY,//ChengYu@20131029, turn on weekly timer
#if AOV_TIMER_MOD_MON_TO_FRI
		RS_MONDAY_TO_FRIDAY,
#endif
/*		RS_SYSTEM_TIMERSET_MONTHLY,
		RS_SYSTEM_TIMERSET_YEARLY,*/
};

UINT16	TimerServiceStr_id[]  = 
{
		RS_SYSTEM_TIMERSET_CHANNEL,
#ifdef DVR_PVR_SUPPORT
		RS_RECORD,
#endif		
		RS_SYSTEM_TIMERSET_MESSAGE,
};

UINT16	TimerServiceTxtStr_id[]  = 
{
		RS_SYSTEM_TIMERSET_WAKEUP_CHANNEL,
#ifdef DVR_PVR_SUPPORT
		RS_SYSTEM_TIMERSET_WAKEUP_CHANNEL,/* Need to change to "Record channel" */
#endif				
		RS_SYSTEM_TIMERSET_WAKEUP_MESSAGE,
};

UINT16	WakeupMessageStr_id[]  = 
{
		RS_SYSTEM_TIMERSET_MSG_BIRTHDAY,
		RS_SYSTEM_TIMERSET_MSG_ANNIVERSARY,
		RS_SYSTEM_TIMERSET_MSG_GENERAL,
};

UINT16	TimerRecType_id[]	=
{
#if defined (SUPPORT_CAS9) || defined(SUPPORT_CAS7)
		0,
		0,
#else
		RS_COMMON_NO,//RS_INFO_TS,
		RS_COMMON_YES,//RS_INFO_PS,
#endif		
};

void win_timerlist_loadsetting(void);

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/
static VACTION timerlst_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
	case V_KEY_ENTER:
	case V_KEY_RIGHT:		
		act = VACT_ENTER;
		break;
	default:
		act = VACT_PASS;
	}

	return act;
}

static PRESULT timerlst_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	UINT8 bID;
	TIMER_SET_CONTENT rettimer,*timer;
	SYSTEM_DATA* sys_data;
	P_NODE p_node;
	UINT16 curview_index = 0;


	bID = OSD_GetObjID(pObj);
	sys_data = sys_data_get();
	

	switch(event)
	{
	case EVN_PRE_DRAW:
		break;
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
#if Maker_GUI_OFF
		ret = win_timer_list_unkown_act_proc(pObj,unact);
#else
		if(unact == VACT_ENTER)
		{
			sys_data->timer_set.common_timer_num = bID;
			
			timer = &sys_data->timer_set.TimerContent[bID - 1];
			if(timer->timer_mode==TIMER_MODE_OFF)
			{
				timer->wakeup_chan_mode = sys_data_get_cur_chan_mode();
				curview_index = sys_data_get_cur_group_cur_mode_channel();
				get_prog_at(curview_index,&p_node);
				timer->wakeup_channel = p_node.prog_id;
			}/*if the timer if off,set the default channel as current playing one.*/
			
			if(win_timerset_open(timer,&rettimer,timer->timer_mode==TIMER_MODE_OFF))
			{
				
#if (defined(SUPPORT_CAS9) || defined(SUPPORT_CAS7))
				//get_prog_by_id(rettimer.wakeup_channel, &p_node);
				if(/*p_node.ca_mode &&*/ (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD) 
						&&(rettimer.timer_mode != TIMER_MODE_OFF) && ca_is_card_inited())
				{
					#ifdef CAS9_V6 //20130704#2_cache_pin
					check_cached_ca_pin();					
					MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT)); /* store timer record info. */
					#else
					
					BOOL old_value = ap_enable_key_task_get_key(TRUE);
					BOOL save_pin = win_matpop_open_ext(NULL, 0, MAT_RATING_FOR_LIVE,rettimer.wakeup_channel);
					if(save_pin)
					{
						MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));   
						timer->preset_pin = 1;
						MEMCPY(&(timer->pin[0]),get_input_mat_chars(),PWD_LENGTH);
                        
					}
					else
					{
						timer->preset_pin = 0;
					}
					ap_enable_key_task_get_key(old_value);
					
					#endif				
				}
				else if((!ca_is_card_inited()) && (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD)
					&&(rettimer.timer_mode != TIMER_MODE_OFF))
				{
					UINT8 back_saved;
					win_compopup_init(WIN_POPUP_TYPE_SMSG);
					win_compopup_set_msg_ext("Pls check your card!", NULL, 0);
					win_compopup_open_ext(&back_saved); 
					osal_task_sleep(3000);
					win_compopup_smsg_restoreback();
				}
				
				else
				{
				MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));  
				}

#elif defined(VMX_PVR_SUPPORT)
				//get_prog_by_id(rettimer.wakeup_channel, &p_node);
				if(/*p_node.ca_mode &&*/ (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD) 
						&&(rettimer.timer_mode != TIMER_MODE_OFF) && vmx_check_sc() )
				{
					static UINT32 default_key_exit[] = 
					{
						V_KEY_EXIT,V_KEY_ENTER,V_KEY_UP, V_KEY_DOWN
					};
					BOOL save_pin = TRUE;
					UINT8 cache_sc_pin[PWD_LENGTH] = {0}, cache_nsc_pin[PWD_LENGTH] = {0};

					#ifndef VMX_NSC_ONLY
					save_pin = win_cache_pin_open(default_key_exit, ARRAY_SIZE(default_key_exit), 0, cache_sc_pin);
					if(save_pin)
					{
						libc_printf("save_pin, pin = %x, %x, %x, %x\n", cache_sc_pin[0], cache_sc_pin[1], cache_sc_pin[2], cache_sc_pin[3]);
						MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));   
						timer->preset_pin = 1;
                        MEMCPY(&(timer->pin[0]), cache_sc_pin, PWD_LENGTH);
					}
					else
					{
						timer->preset_pin = 0;
				}
					#endif

					#if 1 //cache non-smartcard pin
					if(BC_CheckNSc()==0)
					{
						save_pin = win_cache_pin_open(default_key_exit, ARRAY_SIZE(default_key_exit), 3, cache_nsc_pin);
						if(save_pin)
						{
							MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));   
							timer->preset_nsc_pin = 1;
	                        MEMCPY(&(timer->nsc_pin[0]), cache_nsc_pin, PWD_LENGTH);
						}
						else
						{
							timer->preset_nsc_pin = 0;
						}
					}
					#endif
				}
				else if((!vmx_check_sc()) && (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD)
					&&(rettimer.timer_mode != TIMER_MODE_OFF))
				{
					UINT8 back_saved;
					win_compopup_init(WIN_POPUP_TYPE_SMSG);
					win_compopup_set_msg_ext("Pls check your card!", NULL, 0);
					win_compopup_open_ext(&back_saved); 
					osal_task_sleep(3000);
					win_compopup_smsg_restoreback();
				}
				else
				{
					MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));  
				}
#else
                MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));	
#endif			
			}
			OSD_TrackObject((POBJECT_HEAD)&g_win_timerlist, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
		}
#endif		
		break;
	default:
		;
		
		
	}

	return ret;
}


#if Maker_GUI_OFF
static PRESULT win_timer_list_unkown_act_proc(POBJECT_HEAD pObj,VACTION act)
{
	PRESULT ret = PROC_LOOP;
	UINT8 bID;
	TIMER_SET_CONTENT rettimer,*timer;
	SYSTEM_DATA* sys_data;
	P_NODE p_node;
	UINT16 curview_index = 0;
	UINT32 count = sys_data_get_group_num();

	

	bID = OSD_GetObjID(pObj);
	sys_data = sys_data_get();


	switch(act)
	{
		case VACT_ENTER:
		{
			sys_data->timer_set.common_timer_num = bID;
			
			timer = &sys_data->timer_set.TimerContent[bID - 1];
			if(timer->timer_mode==TIMER_MODE_OFF)
			{
				timer->wakeup_chan_mode = sys_data_get_cur_chan_mode();
				curview_index = sys_data_get_cur_group_cur_mode_channel();
				get_prog_at(curview_index,&p_node);
				timer->wakeup_channel = p_node.prog_id;
			}/*if the timer if off,set the default channel as current playing one.*/
			
			if(win_timerset_open(timer,&rettimer,timer->timer_mode==TIMER_MODE_OFF))
			{
				
#if (defined(SUPPORT_CAS9) || defined(SUPPORT_CAS7))
				//get_prog_by_id(rettimer.wakeup_channel, &p_node);
				if(/*p_node.ca_mode &&*/ (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD) 
						&&(rettimer.timer_mode != TIMER_MODE_OFF) && ca_is_card_inited())
				{
					#ifdef CAS9_V6 //20130704#2_cache_pin
					check_cached_ca_pin();					
					MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT)); /* store timer record info. */
					#else
					
					BOOL old_value = ap_enable_key_task_get_key(TRUE);
					BOOL save_pin = win_matpop_open_ext(NULL, 0, MAT_RATING_FOR_LIVE,rettimer.wakeup_channel);
					if(save_pin)
					{
						MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));   
						timer->preset_pin = 1;
						MEMCPY(&(timer->pin[0]),get_input_mat_chars(),PWD_LENGTH);                        
					}
					else
					{
						timer->preset_pin = 0;
					}
					ap_enable_key_task_get_key(old_value);
					
					#endif				
				}
				else if((!ca_is_card_inited()) && (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD)
					&&(rettimer.timer_mode != TIMER_MODE_OFF))
				{
					UINT8 back_saved;
					win_compopup_init(WIN_POPUP_TYPE_SMSG);
					win_compopup_set_msg_ext("Pls check your card!", NULL, 0);
					win_compopup_open_ext(&back_saved); 
					osal_task_sleep(3000);
					win_compopup_smsg_restoreback();
				}
				
				else
				{
				MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));  
				}

#elif defined(VMX_PVR_SUPPORT)
				//get_prog_by_id(rettimer.wakeup_channel, &p_node);
				if(/*p_node.ca_mode &&*/ (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD) 
						&&(rettimer.timer_mode != TIMER_MODE_OFF) && vmx_check_sc() )
				{
					static UINT32 default_key_exit[] = 
					{
						V_KEY_EXIT,V_KEY_ENTER,V_KEY_UP, V_KEY_DOWN
					};
					BOOL save_pin = TRUE;
					UINT8 cache_sc_pin[PWD_LENGTH] = {0}, cache_nsc_pin[PWD_LENGTH] = {0};

					#ifndef VMX_NSC_ONLY
					save_pin = win_cache_pin_open(default_key_exit, ARRAY_SIZE(default_key_exit), 0, cache_sc_pin);
					if(save_pin)
					{
						libc_printf("save_pin, pin = %x, %x, %x, %x\n", cache_sc_pin[0], cache_sc_pin[1], cache_sc_pin[2], cache_sc_pin[3]);
						MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));   
						timer->preset_pin = 1;
                        MEMCPY(&(timer->pin[0]), cache_sc_pin, PWD_LENGTH);
					}
					else
					{
						timer->preset_pin = 0;
					}
					#endif

					#if 1 //cache non-smartcard pin
					if(BC_CheckNSc()==0)
					{
						save_pin = win_cache_pin_open(default_key_exit, ARRAY_SIZE(default_key_exit), 3, cache_nsc_pin);
						if(save_pin)
						{
							MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));   
							timer->preset_nsc_pin = 1;
	                        MEMCPY(&(timer->nsc_pin[0]), cache_nsc_pin, PWD_LENGTH);
						}
						else
						{
							timer->preset_nsc_pin = 0;
						}
					}
					#endif
				}
				else if((!vmx_check_sc()) && (rettimer.timer_service == TIMER_SERVICE_DVR_RECORD)
					&&(rettimer.timer_mode != TIMER_MODE_OFF))
				{
					UINT8 back_saved;
					win_compopup_init(WIN_POPUP_TYPE_SMSG);
					win_compopup_set_msg_ext("Pls check your card!", NULL, 0);
					win_compopup_open_ext(&back_saved); 
					osal_task_sleep(3000);
					win_compopup_smsg_restoreback();
				}
				else
				{
					MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));  
				}
#else
                MEMCPY(timer,&rettimer,sizeof(TIMER_SET_CONTENT));	
#endif			
			}
			OSD_TrackObject((POBJECT_HEAD)&g_win_timerlist, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
		}
		break;
	}

	return ret;
}
#endif

static VACTION timerlst_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
  	case V_KEY_UP:
        act = VACT_CURSOR_UP;
        break;
    case V_KEY_DOWN:
        act = VACT_CURSOR_DOWN;
		break;
	case V_KEY_LEFT:
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:
		act = VACT_PASS;
	}

	return act;	

}

static PRESULT timerlst_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;

	switch(event)
	{
	case EVN_PRE_OPEN:
#if Maker_GUI_OFF
		if (!timermenu_set_items(timer_menu_desc))
			return PROC_LEAVE;
#endif	
		wincom_open_title(pObj,RS_SYSTEM_TIMER_SETTING/*RS_SYSTEM_TIMER*/,0);
		break;
	case EVN_POST_OPEN:
		break;
	case EVN_PRE_CLOSE:
		sys_data_check_channel_groups();
		reset_group();
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;
	case EVN_POST_CLOSE:
        sys_data_save(1);
		break;
	case EVN_PRE_DRAW:
		win_timerlist_loadsetting();
		break;
    case EVN_MSG_GOT:
        if(param1 == CTRL_MSG_SUBTYPE_CMD_UPDATE_VIEW)
        {
            OSD_TrackObject(pObj, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
            ret = PROC_LOOP;
        }
        break;
	}


	return ret;
}

#if Maker_GUI_OFF
BOOL timermenu_set_items(sub_menu_t* sub_menu)
{
	UINT32 i;
	sub_menu_item_t* menu_item;
	CONTAINER* menu_item_obj;
	POBJECT_HEAD pObj,pObjName,pObjSet;
	CONTAINER* win_timer_menu = &g_win_timerlist;
	UINT8 action;
    BOOL ret = TRUE;

    if (sub_menu->item_count <= C_TIMERMENU_PAGE)
    {
        sub_menu->list_page = sub_menu->item_count;
    }
    else
    {
        sub_menu->list_page = C_TIMERMENU_PAGE;
    }

    UINT32 count = sub_menu->item_count;
    UINT32 depth = sub_menu->list_page;
    UINT32 max_top = OSD_GetMaxTopPos(count, &depth);

    if (sub_menu->list_top >= sub_menu->list_sel)
        sub_menu->list_top = sub_menu->list_sel;
    else if (sub_menu->list_sel - sub_menu->list_top >= sub_menu->list_page)
        sub_menu->list_top = sub_menu->list_sel + 1 - sub_menu->list_page;

    if (sub_menu->list_top > max_top)
        sub_menu->list_top = max_top;

    if (timermenu_item_attr(sub_menu, sub_menu->list_sel) != C_ATTR_ACTIVE)
    {
        ret = timermenu_scroll_down(sub_menu);
    }

	timer_menu_desc = sub_menu;

	/*Set menu callback */
	if(sub_menu->menu_callback != NULL)
		OSD_SetCallback(win_timer_menu, sub_menu->menu_callback);
	else
		OSD_SetCallback(win_timer_menu, timerlst_callback);		

    timermenu_set_one_page(sub_menu);

	SCROLL_BAR* sb = &timer_scrollbar;
	OSD_SetScrollBarMax(sb, sub_menu->item_count);
	OSD_SetScrollBarPos(sb, sub_menu->list_sel);

    return ret;
}

UINT8 timermenu_item_attr(sub_menu_t* sub_menu, UINT32 index)
{
	UINT8 action;

	if(sub_menu->check_item_active != NULL)
		action = sub_menu->check_item_active(index+1);
	else
		action = C_ATTR_ACTIVE;

    return action;
}

BOOL timermenu_scroll_down(sub_menu_t* sub_menu)
{
    UINT32 count = sub_menu->item_count;
    UINT32 depth = sub_menu->list_page;
    UINT32 top_pos = sub_menu->list_top;
    UINT32 cur_sel = sub_menu->list_sel;
    UINT32 cur_pos = cur_sel - top_pos;
    //UINT32 old_sel = cur_sel;
    //UINT32 old_top = top_pos;
    UINT32 i;
    BOOL ret = TRUE;

    for (i = 0; i < count; i++)
    {
        ret = OSD_ScrollLineDown(count, depth, &top_pos, &cur_pos);
        if (!ret)
            return FALSE;

        cur_sel = top_pos + cur_pos;
        if (timermenu_item_attr(sub_menu, cur_sel) == C_ATTR_ACTIVE)
        {
            sub_menu->list_top = top_pos;
            sub_menu->list_sel = cur_sel;
            return TRUE;
        }
    }
    return FALSE;
}

BOOL timermenu_set_one_page(sub_menu_t* sub_menu)
{
	UINT16 i;
	sub_menu_item_t* menu_item;
	CONTAINER* menu_item_obj;
	POBJECT_HEAD pObj,pObjName,pObjSet;
	CONTAINER* win_timer_menu = &g_win_timerlist;
	UINT8 action;
	BOOL bSelInActive = FALSE;

	for(i=0;i<sub_menu->list_page;i++)
	{

		/* Set attribute to active*/
		action = timermenu_item_attr(sub_menu, i+sub_menu->list_top);
		timermenu_set_item_attr(i,action);
		
		menu_item = &sub_menu->items[i+sub_menu->list_top];
		menu_item_obj = timers_conts[i];

		/* Link menu items next object and set IDs*/
		menu_item_obj->head.pNext = (i< (sub_menu->list_page - 1) ) ?  ((POBJECT_HEAD)timers_conts[i + 1]) : NULL;
		OSD_SetID(menu_item_obj, i + 1, i + 1, i + 1, 
			i==0? sub_menu->list_page : i, 
			(i==sub_menu->list_page - 1)? 1 : i+2);


		/* Set menu item's name display*/
		pObjName = OSD_GetContainerNextObj(menu_item_obj);
		OSD_SetTextFieldContent((PTEXT_FIELD)pObjName, STRING_ID, (UINT32)menu_item->item_name);		

		/*Set menu item's callback */
		if(menu_item->item_callback != NULL)
			OSD_SetCallback(menu_item_obj, menu_item->item_callback);
		else
			OSD_SetCallback(menu_item_obj, timerlst_item_callback);
		if (action != C_ATTR_ACTIVE && sub_menu->list_sel == (i+sub_menu->list_top))
		{
			bSelInActive = TRUE;
		}
		
	}
	// if the focus id is the in active id, choose a active id for
	// focus id;
	if (bSelInActive) 
	{
		for(i=0;i<sub_menu->list_page;i++)
		{
			action = timermenu_item_attr(sub_menu, i+sub_menu->list_top);
			if (C_ATTR_ACTIVE == action)
			{
				sub_menu->list_sel = i;
				break;
			}
		}
	}

	OSD_SetContainerFocus(win_timer_menu, sub_menu->list_sel + 1 - sub_menu->list_top);

}

void timermenu_set_item_attr(UINT32 itemidx, UINT8 bAttr)
{
	CONTAINER* menu_item_obj;
	POBJECT_HEAD pObj;
	
	menu_item_obj = timers_conts[itemidx];
	OSD_SetAttr(menu_item_obj, bAttr);
	pObj = menu_item_obj->pNextInCntn;
	while(pObj != NULL)
	{
		OSD_SetAttr(pObj, bAttr);
		pObj = pObj->pNext;
	}
	
}

#endif

void win_timerlist_loadsetting(void)
{
    SYSTEM_DATA* sys_data;
    TIMER_SET_CONTENT* timer;
    INT32 i;
    CONTAINER* con;
    TEXT_FIELD* txt;
    UINT16* pstr;
    char str[30];
    UINT16 strID;
    P_NODE p_node;

    sys_data = sys_data_get();
    for(i=0;i < MAX_TIMER_NUM;i++)
    {
	LOAD_ONE_TIMER:    
        timer = &sys_data->timer_set.TimerContent[i];
        con = timers_conts[i];
        
        pstr = display_strs[2*i];
        
        /*Number */
        txt = (TEXT_FIELD*)OSD_GetContainerNextObj(con);
		OSD_SetTextFieldStrPoint(txt,pstr);
        OSD_SetTextFieldContent(txt, STRING_NUMBER, i+1);
        pstr += 3;

        /*Mode */
        txt = (TEXT_FIELD*)OSD_GetObjpNext(txt);
        strID = TimerModeStr_id[timer->timer_mode];
        OSD_SetTextFieldContent(txt, STRING_ID,(UINT32)strID);
        
        /*Service */
        txt = (TEXT_FIELD*)OSD_GetObjpNext(txt);
        if(timer->timer_mode!= TIMER_MODE_OFF)
            strID = TimerServiceStr_id[timer->timer_service];
        else
            strID = 0;
       OSD_SetTextFieldContent(txt, STRING_ID,(UINT32)strID);
        
        /*Content */
        strID = 0;
        txt = (TEXT_FIELD*)OSD_GetObjpNext(txt);
        if(timer->timer_mode!= TIMER_MODE_OFF)
        {
            if(timer->timer_service == TIMER_SERVICE_MESSAGE)
            {
                strID = WakeupMessageStr_id[timer->wakeup_message];
                pstr = NULL;
            }
            else
            {
				if(get_prog_by_id(timer->wakeup_channel,&p_node) == DB_SUCCES)
				{
					if(p_node.ca_mode)
						ComAscStr2Uni("$", pstr);
					ComUniStrCopyChar((UINT8*)&pstr[p_node.ca_mode], p_node.service_name);	
				}
                else
                {
                	/* When the specified channel not exit, turn off the timer */
                	timer->timer_mode = TIMER_MODE_OFF;
                    pstr = NULL;
					goto LOAD_ONE_TIMER;
                }
            }
        }
        else
            pstr = NULL;
		OSD_SetTextFieldContent(txt, STRING_ID,(UINT32)strID);
		OSD_SetTextFieldStrPoint(txt,pstr);				


        pstr = display_strs[2*i + 1];
        
        /*Date&Time*/
        txt = (TEXT_FIELD*)OSD_GetObjpNext(txt);
        str[0] = '\0';
        if(timer->timer_mode!= TIMER_MODE_OFF)
            sprintf(str,"%02d/%02d/%04d %02d:%02d",
                timer->wakeup_month,timer->wakeup_day,timer->wakeup_year,
                timer->wakeup_time/60,timer->wakeup_time%60);
		OSD_SetTextFieldStrPoint(txt,pstr);
		OSD_SetTextFieldContent(txt, STRING_ANSI,(UINT32)str);
        pstr += 20;

        /*Duration*/
        txt = (TEXT_FIELD*)OSD_GetObjpNext(txt);
        str[0] = '\0';
        if(timer->timer_mode!= TIMER_MODE_OFF 
			&& (timer->timer_service == TIMER_SERVICE_CHANNEL
#ifdef DVR_PVR_SUPPORT
			|| timer->timer_service == TIMER_SERVICE_DVR_RECORD
#endif
			)
		)
            sprintf(str,"%02d:%02d",timer->wakeup_duration_time/60,timer->wakeup_duration_time%60);
        ComAscStr2Uni(str, pstr);
        OSD_SetTextFieldStrPoint(txt,pstr);
		OSD_SetTextFieldContent(txt, STRING_ANSI,(UINT32)str);
    }

#if 0
	sys_data_set_cur_chan_mode(cur_chan_mode);	
	recreate_prog_view(VIEW_ALL | cur_chan_mode,0);
#endif	

}

 
